package cn.xz.zysjz.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Build;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import cn.xz.zysjz.R;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.databinding.ActivityMainBinding;
import cn.xz.zysjz.databinding.ActivityWebBinding;

/**
 * 通用的显示网页端界面*/
public class WebActivity extends AppCompatActivity {

    //绑定xml布局文件-也就是手机上看到的界面
    ActivityWebBinding mBinding;
    //网页地址
    private String mUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_web);
        mUrl = getIntent().getStringExtra(CommonLib.JUMP_TYPE);
        //设置网页容器的一些基本数据
        initWebView();
        //显示对应的链接
        mBinding.wvWeb.loadUrl(mUrl);
        //点击事件
        initEvent();
    }

    private void initEvent(){
        mBinding.viewBack.setOnClickListener(v->{
            finish();
        });
    }

    private void initWebView() {
        WebSettings webSettings = mBinding.wvWeb.getSettings();
        // 让WebView能够执行javaScript
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        //mViewDataBinding.webView.webViewClient = webClient//支付
        mBinding.wvWeb.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (mUrl.contains(title)) {
                    mBinding.tvTitle.setText("安疫行");
                } else {
                    mBinding.tvTitle.setText(title);
                }
            }
        });
        // 让JavaScript可以自动打开windows
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        if (Build.VERSION.SDK_INT >= 17) {
            webSettings.setMediaPlaybackRequiresUserGesture(false);
        }
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setLoadWithOverviewMode(true);
        // 设置缓存模式,一共有四种模式
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        // 设置缓存路径
        // webSettings.setAppCachePath("");
        // 支持缩放(适配到当前屏幕)
        webSettings.setSupportZoom(true);
        // 将图片调整到合适的大小
        webSettings.setUseWideViewPort(true);
        // 支持内容重新布局,一共有四种方式
        // 默认的是NARROW_COLUMNS
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        // 设置可以被显示的屏幕控制
        webSettings.setDisplayZoomControls(true);
        // 设置默认字体大小
        webSettings.setDefaultFontSize(40);
        WebView.setWebContentsDebuggingEnabled(true);
        //WebView视频自动播放
        webSettings.setMediaPlaybackRequiresUserGesture(false);
        //监听界面内跳转
        mBinding.wvWeb.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String request) {
                view.loadUrl(request); // 使用当前WebView处理跳转
                return true; // true表示此事件在此处被处理，不需要再广播
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (mBinding.wvWeb.canGoBack()){
            mBinding.wvWeb.goBack();
        }else {
            mBinding.wvWeb.removeAllViews();
            mBinding.wvWeb.destroy();
            finish();
        }
    }
}